package com.example.myshop.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myshop.domain.CommentDao;
import com.example.myshop.entity.Comment;
import com.example.myshop.service.CommentService;
import com.example.myshop.service.ex.InsertException;
import com.example.myshop.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImp extends ServiceImpl<CommentDao, Comment> implements CommentService {
    @Autowired
    private CommentDao commentDao;


    @Override
    public void insert(Comment comment) {
        Date date =new Date();
        comment.setCreatedTime(date);
        comment.setModifiedTime(date);
        Integer rows=commentDao.insert(comment);
        if(rows!=1){
            throw new InsertException("评论记录异常");
        }
    }

    @Override
    public void updateByBid(Long uid, Long bid, Long oid, Long pid, String reply) {
        UpdateWrapper<Comment>updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("reply",reply);
        updateWrapper.eq("uid",uid);
        updateWrapper.eq("bid",bid);
        updateWrapper.eq("oid",oid);
        updateWrapper.eq("pid",pid);
        Integer rows=commentDao.update(null,updateWrapper);
        if(rows!=1){
            throw new UpdateException("评论记录更新异常");
        }
    }

    @Override
    public List<Comment> findByUid(Long uid) {
        QueryWrapper<Comment>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        List<Comment> result = commentDao.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<Comment> findByBid(Long bid) {
        QueryWrapper<Comment>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("bid",bid);
        List<Comment> result = commentDao.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<Comment> findByPid(Long pid) {
        QueryWrapper<Comment>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pid",pid);
        List<Comment> result = commentDao.selectList(queryWrapper);
        return result;
    }
}
